<?php

class BaseAction extends Action
{

    //根据reportid获取reportinfo表内容
    public function reportInfo($reportid)
    {
        $Reportinfo = M("Reportinfo");
        $condition['reportid'] = array('eq', $reportid);
        $reportlist = $Reportinfo->where($condition)->select();
        return $reportlist[0]; //fix 为空时候判断
    }

    //根据reportid获取programinfo表内容
    public function programInfo($reportid)
    {
        $Programinfo = M("Programinfo");
        $condition['reportid'] = array('eq', $reportid);
        $reportlist = $Programinfo->where($condition)->select();
        return $reportlist;
    }

    //完整总小项
    public function getAllItemInfo($reportid)
    {
        $Iteminfo = M("Iteminfo");

        $condition['reportid'] = array('eq', $reportid);

        $iteminfolist = $Iteminfo->where($condition)->select();
        return $iteminfolist;
    }

    //真正总小项
    public function getItemInfo($reportid)
    {
        $Iteminfo = M("Iteminfo");

        $condition['reportid'] = array('eq', $reportid);

        $condition['itemname'] = array(array('neq', '总检'), array('neq', '描述'), array('neq', '小结'), 'and');

        $iteminfolist = $Iteminfo->where($condition)->select();
        return $iteminfolist;
    }

    public function getPassItemInfo($items)
    {
        $i1 = 0;
        $newArr = array();
        for ($i = 0; $i < count($items) - 1; $i++)
        {
            if (!$this->isIssueItem($items[$i]))
            {
                $i1 = $i1 + 1;
                $newArr[$i1] = $items[$i];
            }
        }
        return $newArr;
    }

    public function getIssueItemInfo($items)
    {
        $i1 = 0;
        $newArr = array();
        for ($i = 0; $i < count($items) - 1; $i++)
        {
            if ($this->isIssueItem($items[$i]))
            {
                $i1 = $i1 + 1;
                $newArr[$i1] = $items[$i];
            }
        }
        return $newArr;
    }

    public function isIssueItem($obj)
    {
        if (strstr($obj['result'], '龋齿') <> NULL ||
                strstr($obj['result'], '萎缩') <> NULL ||
                strstr($obj['result'], '增生') <> NULL ||
                strstr($obj['result'], '缺失') <> NULL ||
                strstr($obj['result'], '老年性阴道炎') <> NULL ||
                strstr($obj['result'], '分泌物多') <> NULL ||
                strstr($obj['result'], '外痔') <> NULL ||
                strstr($obj['result'], '内痔') <> NULL ||
                $obj['issue'] == '↑' ||
                $obj['issue'] == '↓')
        {
            return TRUE;
        }
        else
        {
            return FALSE;
        }
    }

    //根据项目名称返回小项
    public function getResult($allItems, $itemname)
    {
        for ($i = 0; $i < count($allItems); $i++)
        {
            if ($allItems[$i]['itemname'] == $itemname)
            {
                $value = $allItems[$i]['result'];
                break;
            }
        }
        return $value;
    }

    //过滤空白字符串
    public function filterEmpty($var)
    {
        return strlen(trim($var)) > 0;
    }

    //总汇提取
    public function summary($var)
    {
        
        $result = array();
        
        if (strpos($var, "【") !== false) {
            $itemValue = $var;

            $pattern = "【";

            $spArray = split($pattern, $itemValue);
            $result = Array();
            foreach ($spArray as $one) {
                if ($one) {
                    $tmpArr = split("】", $one);
                    $oneWithout = $tmpArr[1];
                    $result[] = split("：", $oneWithout);
                }
            }
        } else {
            $matches = split('\*', $var);

            for ($i = 1; $i < count($matches) - 1; $i++) {
                $bigone = $matches[$i];

                $smallones = split('<br\/>', $bigone);
                $arrTmp = array();
                foreach ($smallones as $key => $value) {

                    $value = trim($value);
                    if ($key == 0) {
                        $value = str_replace(':', '', $value);
                    }
                    if ("" !== $value) {
                        $arrTmp[] = $value;
                    }
                }

                if (count($arrTmp) == 1) {
                    $arrTmp[1] = '其他';
                    $arrTmp = array_reverse($arrTmp);
                }

                $result[] = $arrTmp;
            }
        }



        return $result;
    }

    /**
     * 器官总汇
     * 1 - 肺 ; 2 - 心脏 ; 3 - 肝脏 ; 4 - 胰腺 ; 5 - 脾 ; 
     * 6 - 胃 ; 7 - 胆 ; 8 - 肾脏 ; 9 - 肠 ; 10 - 子宫 ;
     *  11 - 膀胱 ;
     */
    public function totalOrganStatus($allRealItems)
    {
        $result = array();
        for ($i = 1; $i <= 11; $i++)
        {
            $result[$i] = $this->singleOrganStatus($i, $allRealItems);
        }
        return $result;
    }

    public function issueOrgancount($arr)
    {
        $result = array();
        foreach ($arr as $organid => $singleOrgan)
        {
            $issueArray = $singleOrgan[0];
            $result[$organid] = count($issueArray);
        }

        return $result;
    }

    public function organItemCount($arr)
    {
        $result = array();
        foreach ($arr as $organid => $singleOrgan)
        {
            $issueArray = $singleOrgan[0];
            $normalArray = $singleOrgan[1];
            $result[$organid] = count($issueArray) + count($normalArray);
        }

        return $result;
    }

    /**
     * 单个器官情况
     */
    public function singleOrganStatus($organid, $allRealItems)
    {
        $singleResult = array();
        // 1, 拿到改器官对应的体检项目以及别名
        $OrganMap = M("Organmap");
        $condition['organid'] = array('eq', $organid);
        $mapList = $OrganMap->where($condition)->select();
        // 2. 过一遍allRealItems, 看是否是相关项目
        $normalItems = array();
        $issueItems = array();

        foreach ($allRealItems as $item)
        {
            if ($this->isRelatedItem($item, $mapList))
            {
                
                if ($this->isIssueItem($item))
                {
                    $issueItems[] = $this->getOrganType($item);
                }
                else
                {
                    $normalItems[] = $this->getOrganType($item);
                }
            }
        }

        // 3. 是, 并且是异常, 放在0位置
        $singleResult[0] = $issueItems;

        // 4. 是, 并且是正常, 放在1位置
        $singleResult[1] = $normalItems;
       
        return $singleResult;
    }

    /*
     * 1. 定量 从0  正常
     * 2. 定量 从0  异常
     * 3. 定量 不从0 正常
     * 4. 定量 不从0 异常 偏小
     * 5. 定量 不从0 异常 偏大
     * 6. 定性 正常
     * 7. 定性 异常
     */

    public function getOrganType($item)
    {
        $normalStr = $item['normal'];
        if (strpos($normalStr, "~") !== FALSE || strpos($normalStr, "-") !== FALSE && !preg_match("/[\x7f-\xff]/", $normalStr)) //定量指标 不包含中文
        {
            $scopes = split('-', $normalStr);
            $leftValue = $scopes[0];
            if ($leftValue == "0")
            {
                if ($item['issue'] == "↑")
                {
                    $item["type"] = 2;
                }
                else
                {
                    $item["type"] = 1;
                }
            }
            else
            {
                if ($item['issue'] == "↑")
                {
                    $item["type"] = 5;
                }
                else if ($item['issue'] == "↓")
                {
                    $item["type"] = 4;
                }
                else
                {
                    $item["type"] = 3;
                }
            }
        }
        else
        {
            if ($this->isIssueItem($item))
            {
                $item["type"] = 7;
            }
            else
            {
                $item["type"] = 6;
            }
        }

        return $item;
    }

    /**
     * 判断单个项目是否是某个器官的相关项目
     */
    function isRelatedItem($item, $mapList)
    {
        $itemname = $item["itemname"];
        $itemname = preg_replace('/ /i', '', $itemname);
//        $itemname = preg_replace('/[A-Z]/i', '', $itemname);
//        $itemname = preg_replace('/\(/i', '', $itemname);
//        $itemname = preg_replace('/\)/i', '', $itemname);
        foreach ($mapList as $mapObj)
        {
            $thename = $mapObj["itemname"];
            $alias = $mapObj["itemalias"];
            
            if (strpos($thename, $itemname) !== false || $thename == $itemname || in_array($itemname, split('|', $alias)))
            {
                return TRUE;
            }
        }

        return FALSE;
    }

    /**
     * 原始体检报告提取
     */
    public function rawReport($allTotalItems, $allPrograms)
    {

        // 1. 大项名称数组
        $progamNames = array();
        foreach ($allPrograms as $program)
        {
            $progamNames[] = $program["programname"];
        }

        // 2. 大项图标数组
        $icons = array();
        foreach ($allPrograms as $program)
        {
            $name = $program["programname"];
            $icons[] = $this->getIconFromName($name);
        }
        // 3. 小项二维数组
        $smallItems = array();
        foreach ($allPrograms as $program)
        {
            $programid = $program["programid"];
            $smallItems[] = $this->getSmallItemsWithProgramid($allTotalItems, $programid);
        }

        return array($progamNames, $icons, $smallItems);
    }

    /**
     * 根据programid获取相关小项目
     */
    function getSmallItemsWithProgramid($allTotalItems, $programid)
    {
        $result = array();
        foreach ($allTotalItems as $item)
        {
            if ($item["programid"] == $programid)
            {
                if ($this->isIssueItem($item) && $item["issue"] != "↓" && $item["issue"] != "↑")
                {
                    $item["issue"] = $item["result"];
                }
                $item = $this->getOrganType($item);
                $result[] = $item;
            }
        }
        return $result;
    }

    /**
     * 获取icon名称
     */
    function getIconFromName($name)
    {
        //一般检查|基础检查|一般检查(1)
        if (strpos($name, "一般") !== FALSE || strpos($name, "基础") !== FALSE)
        {
            return "一般检查.png";
        }

        // 内科
        if (strpos($name, "内科") !== FALSE)
        {
            return "内科.png";
        }

        // 外科
        if (strpos($name, "外科") !== FALSE)
        {
            return "外科.png";
        }

        if (strpos($name, "眼科") !== FALSE || strpos($name, "视力") !== FALSE)
        {
            return "眼科.png";
        }

        if (strpos($name, "耳鼻喉") !== FALSE)
        {
            return "耳鼻喉.png";
        }

        if (strpos($name, "口腔") !== FALSE)
        {
            return "口腔.png";
        }

        if (strpos($name, "心电") !== FALSE)
        {
            return "心电.png";
        }

        if (strpos($name, "双颈、椎动脉彩超") !== FALSE)
        {
            return "双颈、椎动脉彩超.png";
        }

        if (strpos($name, "胆红素") !== FALSE)
        {
            return "胆红素.png";
        }

        if (strpos($name, "肝功") !== FALSE)
        {
            return "肝功.png";
        }

        if (strpos($name, "空腹血糖") !== FALSE)
        {
            return "空腹血糖.png";
        }

        if (strpos($name, "尿液") !== FALSE || strpos($name, "尿常规") !== FALSE || strpos($name, "泌尿") !== FALSE)
        {
            return "泌尿.png";
        }

        if (strpos($name, "肾功") !== FALSE)
        {
            return "肾功.png";
        }

        if (strpos($name, "胃功能") !== FALSE)
        {
            return "胃功能.png";
        }

        if (strpos($name, "血清蛋白") !== FALSE || strpos($name, "血免疫") !== FALSE)
        {
            return "血免疫.png";
        }

        if (strpos($name, "血液分析") !== FALSE || strpos($name, "血常规") !== FALSE)
        {
            return "血液分析.png";
        }

        if (strpos($name, "血脂") !== FALSE || strpos($name, "脂蛋白") !== FALSE)
        {
            return "血脂.png";
        }

        if (strpos($name, "妇科") !== FALSE)
        {
            return "妇科.png";
        }

        return "其它.png";
    }

}
